package com.liao.shapping.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.liao.shapping.R;
import com.liao.shapping.activity.ShoppingCartActivity;
import com.liao.shapping.domain.InfoBean;
import com.liao.shapping.view.AmountView;
import com.squareup.picasso.Picasso;

public class ItemShoppingcartListviewLayoutAdapter extends BaseAdapter {

    private List<InfoBean> infoBeans = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;
    private CallBack callBack;

    public ItemShoppingcartListviewLayoutAdapter(Context context, ArrayList<InfoBean> infoBeans,CallBack callBack) {
        this.context = context;
        this.callBack = callBack;
        this.infoBeans = infoBeans;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return infoBeans.size();
    }

    @Override
    public InfoBean getItem(int position) {
        return infoBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_shoppingcart_listview_layout, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final InfoBean infoBean, final ViewHolder holder) {
        holder.amountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                infoBean.setBuycount(amount);
                callBack.changeNum();
            }
        });
        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                infoBeans.remove(infoBean);
                callBack.changeNum();
            }
        });
        int buycount = infoBean.getBuycount();
        int count = buycount == 0 ? 1 : buycount;
        String s = "￥" + infoBean.getPrice() * count;
        holder.amountView.setGoods_storage(infoBean.getCount());
        holder.tvPrice.setText(s);
        holder.amountView.setAmount(count);
        holder.tvShopTitle.setText(infoBean.getTitle());
        holder.tvShopKind.setText(infoBean.getKind());
        Picasso.get().load(infoBean.getPic()).error(R.mipmap.ic_launcher).into(holder.img);
    }

    public interface CallBack {
        void changeNum();
    }

    protected class ViewHolder {
        private ImageView img;
        private TextView tvShopTitle;
        private TextView tvShopKind;
        private AmountView amountView;
        private TextView tvPrice;
        private ImageView imgDelete;

        public ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.img);
            tvShopTitle = (TextView) view.findViewById(R.id.tv_shop_title);
            tvShopKind = (TextView) view.findViewById(R.id.tv_shop_kind);
            amountView = (AmountView) view.findViewById(R.id.amountView);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);
            imgDelete = (ImageView) view.findViewById(R.id.img_delete);
        }
    }
}
